package com.itheima.dubbo.mysql.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.model.domain.UserInfo;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.dubbo.interfaces.UserInfoApi;
import com.itheima.dubbo.mysql.mapper.BlackListMapper;
import com.itheima.dubbo.mysql.mapper.SettingsMapper;
import com.itheima.dubbo.mysql.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    @Autowired
    private BlackListMapper blackListMapper;


    @Override
    public void addUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updateLogo(Long uid, String pictureURL) {
        userInfoMapper.update(UserInfo.builder().logo(pictureURL).build(),
                Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserId, uid));
    }

    @Override
    public UserInfo findUserInfoByUserId(Long uid) {
        return userInfoMapper.selectOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserId, uid));
    }

    @Override
    public Map<Long, UserInfo> findUserInfoListByUserIds(UserInfo userInfo, List<Long> userIds) {
        // 查询条件  没有传递过来
        List<UserInfo> userInfos = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                        .in(UserInfo::getUserId, userIds)
                        .eq(ObjectUtil.isNotNull(userInfo.getSex()), UserInfo::getSex, userInfo.getSex())
//                    .eq(ObjectUtil.isNotNull(userInfo.getCity()), UserInfo::getCity, userInfo.getCity())
//                    .le(ObjectUtil.isNotNull(userInfo.getAge()), UserInfo::getAge, userInfo.getAge())
                        .like(ObjectUtil.isNotNull(userInfo.getNickName()), UserInfo::getNickName, userInfo.getNickName())
        );
        Map<Long, UserInfo> infoMap = CollUtil.fieldValueMap(userInfos, "userId");
        return infoMap;
    }

    @Override
    public Map<Long, UserInfo> findUserInfoListByUserIds(List<Long> userIds) {
        List<UserInfo> userInfos = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(UserInfo::getUserId, userIds));
        Map<Long, UserInfo> infoMap = CollUtil.fieldValueMap(userInfos, "userId");
        return infoMap;
    }

    @Override
    public void updateUserInfoByUserId(UserInfo userInfo) {
        userInfoMapper.update(userInfo, Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getUserId, userInfo.getUserId()));

    }

    @Override
    public List<UserInfo> queryUserInfoByPage(Integer page, Integer pageSize, Long userId, String nickname, String city) {
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(new Page<>(page, pageSize), Wrappers.lambdaQuery(UserInfo.class)
                //like后面的条件是指,如果满足ObjectUtil.isNotNull(userId)就执行
                .like(ObjectUtil.isNotNull(userId), UserInfo::getUserId, userId)
                .like(ObjectUtil.isNotNull(nickname), UserInfo::getNickName, nickname)
                .like(ObjectUtil.isNotNull(city), UserInfo::getCity, city)
        );
        return userInfoPage.getRecords();
    }

    @Override
    public Long queryUserInfo(Long userId, String nickname, String city) {
        List<UserInfo> userInfoList = userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                //like后面的条件是指,如果满足ObjectUtil.isNotNull(userId)就执行
                .like(ObjectUtil.isNotNull(userId), UserInfo::getUserId, userId)
                .like(ObjectUtil.isNotNull(nickname), UserInfo::getNickName, nickname)
                .like(ObjectUtil.isNotNull(city), UserInfo::getCity, city));
        return Convert.toLong(userInfoList.size());
    }


    /**
     * 根据一天的开始时间和结束时间查当天注册人数
     *
     * @param startOfDay
     * @param endOfDay
     * @return
     */
    @Override
    public List<UserInfo> findCountByDateAndGender(Date startOfDay, Date endOfDay, String sex) {
        //判断是否传了性别
        List<UserInfo> userInfos = new ArrayList<>();
        if (StrUtil.isBlank(sex)) {
            //没传
            userInfos = userInfoMapper
                    .selectList(Wrappers.lambdaQuery(UserInfo.class)
                            .between(UserInfo::getCreated, startOfDay, endOfDay));
        } else {
            //传了
            Integer gender = sex.equals("man") ? 1 : 2;
            userInfos = userInfoMapper
                    .selectList(Wrappers.lambdaQuery(UserInfo.class)
                            .between(UserInfo::getCreated, startOfDay, endOfDay)
                            .eq(UserInfo::getSex, gender));
        }
        return userInfos;
    }
    @Override
    public List<UserInfo> findAll() {
        List<UserInfo> userInfos = userInfoMapper.selectList(null);


        return userInfos;
    }



}
